package com.nj.liandi.common.valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer>{

    private static final Logger logger = LoggerFactory.getLogger(ListValueConstraintValidator.class);

    private Set<Integer> set = new HashSet<>();
    //初始化方法
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
//        if (vals.length>0){
//            for (int val : vals) {
//                set.add(val);
//            }
//        }
        for (int val : vals) {
            set.add(val);
        }
        logger.error("=======错误信息{}",vals);
    }

    //判断是否校验成功
//    ConstraintValidatorContext校验的上下文环境信息
//    value需要校验的值
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {

        logger.error("=======错误zhi{}",value);
        return set.contains(value);
    }
}
